package com.roin.learn.activiti.service.impl;

import com.github.pagehelper.PageHelper;
import com.roin.learn.activiti.entity.Page;
import com.roin.learn.activiti.dao.UserMapper;
import com.roin.learn.activiti.entity.User;
import com.roin.learn.activiti.entity.UserExample;
import com.roin.learn.activiti.exception.BusinessException;
import com.roin.learn.activiti.service.UserService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;


@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Resource
	IdentityService identityService;

	public User getById(Integer id) {
		return userMapper.selectByPrimaryKey(id);

	}

	public Page<User> getPage(int pageNum, int pageSize, User user) {
		PageHelper.startPage(pageNum,pageSize);
		UserExample example = new UserExample();
		if (user!=null){
			UserExample.Criteria criteria = example.createCriteria();
			if (user.getUserName()!=null)
				criteria.andUserNameLike(user.getUserName());

		}
		return new Page(userMapper.selectByExample(example));
	}
	public Page<User> getDtoPage(int pageNum, int pageSize, User dto) {
		PageHelper.startPage(pageNum,pageSize);
		UserExample example = new UserExample();
		example.setOrderByClause("user_id asc");
		if (dto!=null){
			UserExample.Criteria criteria = example.createCriteria();
			if (dto.getUserName()!=null)
				criteria.andUserNameLike(dto.getUserName());

		}
		return new Page(userMapper.selectByExampleWithMc(example)) ;
	}

	public int update(User user) {
		org.activiti.engine.identity.User u = identityService.createUserQuery().userId(user.getUserId()).singleResult();
		copyUser(user,u);
		//同步工作流用户
		identityService.saveUser(u);
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public int add(User user) throws BusinessException{
		User userTemp = userMapper.selectByUserId(user.getUserId());
		if (userTemp!=null)
			throw new BusinessException("用户名已存在");
		org.activiti.engine.identity.User u = new UserEntity();
		copyUser(user,u);
		//同步工作流用户
		identityService.saveUser(u);
		return userMapper.insert(user);
	}

	@Override
	public int deleteByUserIds(String[] userIds) {
		for (String userId : userIds) {
			identityService.deleteUser(userId);
		}
		UserExample example = new UserExample();
		example.createCriteria().andUserIdIn(Arrays.asList(userIds));
		return userMapper.deleteByExample(example);
	}

	@Override
	public User getByUserId(String userId) {
		return  userMapper.selectByUserId(userId);
	}

	@Override
	public void syncUser() throws BusinessException {
		userMapper.delActivitiUserNotInUserTable();
		userMapper.updateActivitiUserfromUserTable();
		userMapper.insertActivitiUserfromUserTable();
	}

	/**
	 * 复制用户信息到Actitivi用户表用户里
	 * @param user
	 * @param u
	 */
	private void copyUser(User user, org.activiti.engine.identity.User u){
		u.setId(user.getUserId());
		u.setEmail(user.getUserEmail());
		u.setFirstName(user.getUserId());
		u.setLastName(user.getUserName());
		u.setPassword(user.getUserPassword());
	}
}
